extends IEventActuator


class_name MethodActuator
"""
执行指定的接口
"""

## 执行的接口名
@export var method: String = ""
## 调用时的参数
@export var params: Array = []

@export_category("Option")
## 是否需要使用call_deferred执行
@export var is_deferred: bool = false
## 操作的组件
@export var finder_or_node: Node = null
@export_category("")


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "method": func(): return method.length() > 0,
        "finder_or_node": IComponent.find_child.bind(self, func(x): return x is INodeFinder, finder_or_node),
    }, super.__check_exports())

func get_wrapper() -> Node:
    return finder_or_node.find() if finder_or_node is INodeFinder else finder_or_node

func execute() -> void:
    var node: Node = get_wrapper()
    if not node.has_method(method):
        push_error("MethodActuator node method is not exist:" + method)
        return
    if params.size() > 0:
        node.callv(method, params)
        return
    if is_deferred:
        node.call_deferred(method)
    else:
        node.call(method)
